package com.truyen.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.googlecode.ehcache.annotations.Cacheable;
import com.truyen.persistence.dao.TruyenMaDao;
import com.truyen.persistence.entity.TacGia;
import com.truyen.persistence.entity.ChapterTam;
import com.truyen.persistence.entity.ChapterTruyenMa;
import com.truyen.persistence.entity.ChapterTmpTruyenMa;
import com.truyen.persistence.entity.TruyenMa;
import com.truyen.service.ChapterTamService;
import com.truyen.service.ChapterTruyenMaService;
import com.truyen.service.ChapterTmpTruyenMaService;
import com.truyen.service.TruyenMaService;
import com.truyen.utils.PagingInfo;


@Service
public class TruyenMaServiceImpl implements TruyenMaService {
	
	@Autowired
	private TruyenMaDao truyenMaDao;
	
	@Autowired
	private ChapterTruyenMaService chapterTruyenMaService;
	
	@Autowired
	private ChapterTmpTruyenMaService chapterTmpTruyenMaService;
	
	@Autowired
	private ChapterTamService chapterTamService;

	@Override
	public void save(TruyenMa entity) {
		truyenMaDao.save(entity);
	}

	@Override
	public TruyenMa findById(Long id) {
		return truyenMaDao.findById(id);
	}

	@Override
	public List<TruyenMa> getAll() {
		return truyenMaDao.findAll();
	}

	@Override
	public TruyenMa findByProperty(String column, Object value) {
		List<TruyenMa> lst = truyenMaDao.findByProperty(column, value);
		return (lst.size() > 0)?lst.get(0):null;
	}

	@Override
	public List<TruyenMa> getLstComicLinkImageNullBlogTruyen(int sourceBy){
		return truyenMaDao.getLstComicLinkImageNullBlogTruyen(sourceBy);
	}

	@Override
	@Cacheable(cacheName="truyenMaCacheDataController")
	public List<TruyenMa> getAllLst(String kwSearch, String sort, String sortBy,	PagingInfo pInfo) {
		return truyenMaDao.getAllLst(kwSearch,sort,sortBy,pInfo);
	}

	@Override
	public void setHot(Long id) {
		try{
			TruyenMa entity = findById(id);
			boolean isHot = entity.getIsHot();
			if(isHot){
				entity.setIsHot(false);
			}else{
				entity.setIsHot(true);
			}
			truyenMaDao.merge(entity);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public void setIsDelete(Long id) {
		try{
			TruyenMa entity = findById(id);
			boolean isDelete = entity.getIsDelete();
			if(isDelete){
				entity.setIsDelete(false);
			}else{
				entity.setIsDelete(true);
			}
			truyenMaDao.merge(entity);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	@Override
	@Cacheable(cacheName="lstTruyenMaCacheTruyenController")
	public List<TruyenMa> getListTruyenBy(String orderByColumn,int begin, int max) {
		return truyenMaDao.getListTruyenBy(orderByColumn,begin,max);
	}

	@Override
	public List<TruyenMa> getListTruyenRandom(int maxLimit) {
		return truyenMaDao.getListTruyenRandomBy(maxLimit);
	}

	@Override
	public void updateTotalChapterr(Long id, float totalChapter) {
		try{
			TruyenMa entity = findById(id);
			entity.setTotalChapter(totalChapter);
			truyenMaDao.merge(entity);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public List<TruyenMa> getLstTruyenBySourceBy(int sourceBy) {
		return truyenMaDao.getLstTruyenBySourceBy(sourceBy);
	}

	@Override
	public void updateViews(Long id, long views, long viewsWeek, long viewsMonth) {
		try{
			TruyenMa entity = findById(id);
			entity.setViews(views);
			entity.setViewsWeek(viewsWeek);
			entity.setViewsMonth(viewsMonth);
			truyenMaDao.merge(entity);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	@Cacheable(cacheName="lstTruyenMaTimKiemCacheTruyenController")
	public List<TruyenMa> getLstByProperty(String column, Object value, int begin, int maxLimit) {
		return truyenMaDao.getLstByProperty(column,value, begin, maxLimit);
	}

	@Override
	public List<Object> getObjectByNameUnsigned(String kwSearch, int max, String typeSearch) {
		return truyenMaDao.getObjectByNameUnsigned(kwSearch,max,typeSearch);
	}

	@Override
	public List<TruyenMa> getLstByPropertyWithPaging(String column, String value, String sortBy, PagingInfo pInfo) {
		return truyenMaDao.getLstByPropertyWithPaging(column, value, sortBy, pInfo);
	}

	@Override
	public void updateLiked(Long idTruyen, long liked) {
		try{
			TruyenMa entity = findById(idTruyen);
			entity.setLiked(liked);
			truyenMaDao.merge(entity);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	@Transactional
	public void delete(Long id) {
		try{
			TruyenMa entity = findById(id);
			List<ChapterTam> lstChapterTam = chapterTamService.getByTruyen(entity);
			if(lstChapterTam != null){
				int sizeList = lstChapterTam.size();
				for(int i=0;i<sizeList;i++){
					ChapterTam chapterTam = lstChapterTam.get(i);
					chapterTamService.delete(chapterTam.getId());
				}
			}
			List<ChapterTmpTruyenMa> lstchapterTmpTruyenMa = chapterTmpTruyenMaService.getByTruyen(entity);
			if(lstchapterTmpTruyenMa != null){
				int sizeList = lstchapterTmpTruyenMa.size();
				for(int i=0;i<sizeList;i++){
					ChapterTmpTruyenMa chapterTmpTruyenMa = lstchapterTmpTruyenMa.get(i);
					ChapterTruyenMa chapterTruyenMa = chapterTmpTruyenMa.getChapterTruyenMa();
					chapterTruyenMaService.delete(chapterTruyenMa.getId());
					chapterTmpTruyenMaService.delete(chapterTmpTruyenMa.getId());
				}
			}
			truyenMaDao.delete(entity);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	@Transactional
	public void updateColumnString(long id, String column, String value) {
		try {
			TruyenMa entity = findById(id);
			if(column.equals("linkImage")){
				entity.setLinkImage(value);
			}
			truyenMaDao.merge(entity);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}

	@Override
	@Cacheable(cacheName="lstTruyenMaOrderByCacheTruyenController")
	public List<TruyenMa> getLstByPropertyWithOrderByPaging(String orderBy, PagingInfo pInfo) {
		return truyenMaDao.getLstByPropertyWithOrderByPaging(orderBy,pInfo);
	}

	@Override
	public List<TruyenMa> getLstByTacGiaWithPaging(TacGia tacGia, String orderBy, PagingInfo pInfo) {
		return truyenMaDao.getLstByTacGiaWithPaging(tacGia,orderBy,pInfo);
	}

	@Override
	public List<TruyenMa> getLstByTacGiaLimit(TacGia tacGia, int begin, int maxLimit) {
		return truyenMaDao.getLstByTacGiaLimit(tacGia, begin, maxLimit);
	}

	@Async
	@Override
	public void updateViewsWeek() {
		List<TruyenMa> lstTruyen = getAll();
		if(lstTruyen.size() > 0){
			int size = lstTruyen.size();
			for(int i=0; i < size; i++){
				TruyenMa truyen = lstTruyen.get(i);
				truyen.setViewsWeek(0);
				truyenMaDao.merge(truyen);
			}
		}
	}

	@Async
	@Override
	public void updateViewsMonth() {
		List<TruyenMa> lstTruyen = getAll();
		if(lstTruyen.size() > 0){
			int size = lstTruyen.size();
			for(int i=0; i < size; i++){
				TruyenMa truyen = lstTruyen.get(i);
				truyen.setViewsMonth(0);
				truyenMaDao.merge(truyen);
			}
		}
	}

	@Override
	public void update(Long idTruyen, String name, String alias,
			String linkImage, String nameImage, String info, TacGia tacGia,
			String categories, float totalChapter, boolean isDelete,
			boolean isFinished, boolean isHot, long liked, long views,
			long viewsWeek, long viewsMonth, int sourceBy, String linkSource,
			String translator) {
		try {
			TruyenMa entity = findById(idTruyen);
			if(entity != null){
				entity.setName(name);
				entity.setAlias(alias);
				entity.setLinkImage(linkImage);
				entity.setNameImage(nameImage);
				entity.setInfo(info);
				entity.setTacGia(tacGia);
				entity.setCategories(categories);
				entity.setTotalChapter(totalChapter);
				entity.setIsDelete(isDelete);
				entity.setIsFinished(isFinished);
				entity.setIsHot(isHot);
				entity.setLiked(liked);
				entity.setViews(views);
				entity.setViewsWeek(viewsWeek);
				entity.setViewsMonth(viewsMonth);
				entity.setSourceBy(sourceBy);
				entity.setLinkSource(linkSource);
				entity.setTranslator(translator);
				truyenMaDao.merge(entity);
			}
	} catch (Exception e) {
		// TODO: handle exception
	}
		
	}

}
